<?php
/**
 * GioCMS
 * 
 * LICENSE
 *
 * This source file is subject to the GNU GENERAL PUBLIC LICENSE Version 2 
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://www.gnu.org/licenses/gpl-2.0.txt
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@ninhgio.com so we can send you a copy immediately.
 * 
 * @copyright	Copyright (c) 2010-2011 GioCMS (http://cms.ninhgio.com)
 * @license		http://www.gnu.org/licenses/gpl-2.0.txt GNU GENERAL PUBLIC LICENSE Version 2
 * @author		NinhGio - ninhgio@gmail.com
 * @since		1.0
 */

class Modules_News_Controllers_Update extends Gio_Core_Controller
{
	public function autoAction()
	{
		$this->setNoRender();
		$this->disableLayout();
		
		$auto = new Modules_News_Services_Auto();
		$auto->run();
	}
		
	public function linkAction()
	{
		$this->setNoRender();
		$this->disableLayout();
		
		$request = $this->getRequest();

		$linkId = (int)$request->getParam('link_id');
		$type   = $request->getParam('web_id');
		
		if (!$linkId) {
			return;
		}
		
		$cron = new Modules_News_Services_Cron();
		
		switch ($type) {
			case 'vnexpress':
				$cron->vnexpress($linkId);
				break;
			case 'zing':
				$cron->zing($linkId);
				break;
			case 'dantri':
				$cron->dantri($linkId);
				break;
			case 'megafun':
				$cron->megafun($linkId);
				break;
			case 'kenh14':
				$cron->kenh14($linkId);
				break;
			case 'ngoisao':
				$cron->ngoisao($linkId);
				break;
			case 'haitugio':
				$cron->haitugio($linkId);
				break;
			case 'tuoitre':
				$cron->tuoitre($linkId);
				break;
			case 'thanhnien':
				$cron->thanhnien($linkId);
				break;
			case 'nguoilaodong':
				$cron->nguoilaodong($linkId);
				break;
			case 'haisao':
				$cron->haisao($linkId);
				break;
			case 'autopro':
				$cron->autopro($linkId);
				break;
			case 'bongda':
				$cron->bongda($linkId);
				break;
			case 'vtc':
				$cron->vtc($linkId);
				break;
			case 'tintuconline':
				$cron->tintuconline($linkId);
				break;
		}
		
		$arrayArticles = $cron->addArticles;
		$arrayCategories = $cron->addCategories;
			
		/**
		 * 
		 */
		if (!empty($arrayArticles) && !empty($arrayCategories)) {
			$conn = Gio_Db_Connection::getConnection();
			$articleDao = new Modules_News_Models_Mysql_Article();
			$articleDao->setConnection($conn);
				
			$articleAutoDao = new Modules_News_Models_Mysql_Articleauto();
			$articleAutoDao->setConnection($conn);
						
			for ($i = 0 ; $i < count($arrayArticles); $i++) {
				$addArticles = $arrayArticles[$i];
				$categories = $arrayCategories[$i];
				foreach ($addArticles as $index => $article) {
					$existsArticle = $articleAutoDao->getByLink($article['link_source']);
					
					if (null == $existsArticle) {
						$articleAutoDao->add($article);
					}
				}
			}
		}
	}
}